﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using FluentValidation.Results;
using Microhard.Core;
using Microhard.WST.BI.Buz.Models;
using Microhard.WST.BI.Buz.Msg;
using Microhard.WST.BI.Buz.Interfaces;

namespace Microhard.WST.BI.Buz.Validator
{
    public class C6WorkflowConfigValidator : AbstractValidator<JHFlowInfo>
    {
        public C6WorkflowConfigValidator()
        {
            RuleSet("Insert", () =>
            {
                Custom(u =>
                {
                    if (string.IsNullOrEmpty(u.AppID))
                    {
                        return new ValidationFailure("AppID", JHFlowInfoMsg.NeedAppID);
                    }
                    if (string.IsNullOrEmpty(u.DJSN))
                    {
                        return new ValidationFailure("DJSN", JHFlowInfoMsg.NeedDJSN);
                    }
                    IWorkflowConfig icc = IoC.Resolve<IWorkflowConfig>();
                    if (icc.IsWorkflowTypeCodeDuplicate(u))
                    {
                        return new ValidationFailure("WorkflowTypeCode", JHFlowInfoMsg.DuplicateWorkflowTypeCode);
                    }
                    if (icc.IsAppIDDuplicate(u))
                    {
                        return new ValidationFailure("AppID", JHFlowInfoMsg.DuplicateAppID);
                    }
                    if (icc.IsDJDuplicate(u))
                    {
                        return new ValidationFailure("DJSN", JHFlowInfoMsg.DuplicateDJSN);
                    }
                    return null;
                });

            });

            RuleSet("Update", () =>
            {
                Custom(u =>
                {
                    if (string.IsNullOrEmpty(u.AppID))
                    {
                        return new ValidationFailure("AppID", JHFlowInfoMsg.NeedAppID);
                    }
                    if (string.IsNullOrEmpty(u.DJSN))
                    {
                        return new ValidationFailure("DJSN", JHFlowInfoMsg.NeedDJSN);
                    }
                    IWorkflowConfig icc = IoC.Resolve<IWorkflowConfig>();
                    if (icc.IsWorkflowTypeCodeDuplicate(u))
                    {
                        return new ValidationFailure("WorkflowTypeCode", JHFlowInfoMsg.DuplicateWorkflowTypeCode);
                    }
                    if (icc.IsAppIDDuplicate(u))
                    {
                        return new ValidationFailure("AppID", JHFlowInfoMsg.DuplicateAppID);
                    }
                    if (icc.IsDJDuplicate(u))
                    {
                        return new ValidationFailure("DJSN", JHFlowInfoMsg.DuplicateDJSN);
                    }
                    return null;
                });

            });
        }
    }
}
